package ar.com.sgsvm.dao.sqlite;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import ar.com.sgsvm.dao.IArticuloDao;
import ar.com.sgsvm.dao.SqliteDaoFactory;
import ar.com.sgsvm.entity.Articulo;
import ar.com.sgsvm.exceptions.NonexistentEntityException;

public class ArticuloSqliteDao implements IArticuloDao {

	@Override
	public Long create(Articulo articulo) throws Exception {
		ContentValues contentValues = new ContentValues();
		contentValues.put("idTipoArticulo", articulo.getTipoArticulo().getIdTipoArticulo());
		contentValues.put("codigo", articulo.getCodigo());
		contentValues.put("descripcion", articulo.getDescripcion());
		contentValues.put("stock", articulo.getStock());
		SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
		return sqliteDaoFactory.open().insert("Articulo", null, contentValues);
	}

	@Override
	public List<Articulo> readAll() throws Exception {
		SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
		SQLiteDatabase sqliteDatabase = sqliteDaoFactory.open();
		Cursor cursor = sqliteDatabase.query("Articulo", null, null, null, null, null, null);
		List<Articulo> articulos = new ArrayList<Articulo>();
		if (cursor.moveToFirst()) {
			do {
				Articulo articulo = new Articulo();
				articulo.setIdArticulo(cursor.getLong(cursor.getColumnIndex("idArticulo")));
				articulo.setTipoArticulo(sqliteDaoFactory.getTipoArticuloDao().readById(cursor.getLong(cursor.getColumnIndex("idTipoArticulo"))));
				articulo.setCodigo(cursor.getString(cursor.getColumnIndex("codigo")));
				articulo.setDescripcion(cursor.getString(cursor.getColumnIndex("descripcion")));
				articulo.setStock(cursor.getInt(cursor.getColumnIndex("stock")));
				articulos.add(articulo);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return articulos;
	}

	@Override
	public Articulo readById(Long idArticulo) throws Exception {
		SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
		SQLiteDatabase sqliteDatabase = sqliteDaoFactory.open();
		Cursor cursor = sqliteDatabase.query("Articulo", null, "idArticulo = ?", new String[] { String.valueOf(idArticulo) }, null, null, null);
		if (cursor.moveToFirst()) {
			Articulo articulo = new Articulo();
			articulo.setIdArticulo(idArticulo);
			articulo.setTipoArticulo(sqliteDaoFactory.getTipoArticuloDao().readById(cursor.getLong(cursor.getColumnIndex("idTipoArticulo"))));
			articulo.setCodigo(cursor.getString(cursor.getColumnIndex("codigo")));
			articulo.setDescripcion(cursor.getString(cursor.getColumnIndex("descripcion")));
			articulo.setStock(cursor.getInt(cursor.getColumnIndex("stock")));
			cursor.close();
			return articulo;
		} else {
			throw new NonexistentEntityException("No existe el articulo con id " + String.valueOf(idArticulo));
		}
	}

	@Override
	public void update(Articulo articulo) throws Exception {
		ContentValues contentValues = new ContentValues();
		contentValues.put("idTipoArticulo", articulo.getTipoArticulo().getIdTipoArticulo());
		contentValues.put("codigo", articulo.getCodigo());
		contentValues.put("descripcion", articulo.getDescripcion());
		contentValues.put("stock", articulo.getStock());
		SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
		sqliteDaoFactory.open().update("Articulo", contentValues, "idArticulo = ?", new String[]{String.valueOf(articulo.getIdArticulo())});
	}

	@Override
	public void delete(Articulo articulo) throws Exception {
		SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
		sqliteDaoFactory.open().delete("Articulo", "idArticulo = ?", new String[]{String.valueOf(articulo.getIdArticulo())});
	}

	@Override
	public Articulo readByCodigo(String codigo) throws Exception {
		SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
		SQLiteDatabase sqliteDatabase = sqliteDaoFactory.open();
		Cursor cursor = sqliteDatabase.query("Articulo", null, "codigo = ?", new String[] { codigo }, null, null, null);
		if (cursor.moveToFirst()) {
			Articulo articulo = new Articulo();
			articulo.setIdArticulo(cursor.getLong(cursor.getColumnIndex("idArticulo")));
			articulo.setTipoArticulo(sqliteDaoFactory.getTipoArticuloDao().readById(cursor.getLong(cursor.getColumnIndex("idTipoArticulo"))));
			articulo.setCodigo(codigo);
			articulo.setDescripcion(cursor.getString(cursor.getColumnIndex("descripcion")));
			articulo.setStock(cursor.getInt(cursor.getColumnIndex("stock")));
			cursor.close();
			return articulo;
		} else {
			throw new NonexistentEntityException("No existe el articulo con codigo " + codigo);
		}
	}

}
